package com.thp.bigdata.rjoin.findFriends;

import java.io.IOException;
import java.util.Arrays;

import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Mapper;
/**
 * 这个类是为了测试ChainMapper和MapReducer而改造的一个类
 * @author 汤小萌
 *
 */
public class CommonFriendsStepTwoMapper1 extends Mapper<Text, Text, Text, Text> {

	
	@Override
	protected void map(Text key, Text value, Mapper<Text, Text, Text, Text>.Context context)
			throws IOException, InterruptedException {
		String friend = key.toString();
		String[] users = value.toString().split(",");
		Arrays.sort(users);
		// 以  用户 - 好友 为key，好友 作为value传递给reducer
		for(int i = 0; i < users.length -2; i++) {
			for(int j = i+1; j < users.length -1; j++) {
				/**
		     	 * <B-C,A> <B-D,A> <B-F,A> <B-G,A> <B-H,A> <B-I,A> <B-K,A> <B-O,A>
		     	 * <C-D,A> <C-F,A> <C-G,A> <C-H,A> <C-I,A> <C-K,A> <C-O,A>
		     	 * <D-F,A> <D-G,A> <D-H,A> <D-I,A> <D-K,A> <D-O,A>
		     	 * <F-G,A> <F-H,A> <F-I,A> <F-K,A> <F-O,A>
		     	 * <G-H,A> <G-I,A> <G-K,A> <G-O,A>
		     	 * <H-I,A> <H-K,A> <H-O,A>
		     	 * <I-K,A> <I-O,A>
		     	 * <K-O,A>
				 */
				context.write(new Text(users[i] + "-" + users[j]), new Text(friend));
			}
		}
		
	}
	
}
